package wechat.rcv.controller

import edu.fudan.mylang.pf.IObjectFactory
import java.text.SimpleDateFormat
import org.apache.log4j.Logger
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import com.shrcl.engineerprocess.domain.EngineerProjectAssignment
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RequestMethod
import com.shrcl.engineerprocess.domain.EngineerProjectAssignmentStatus
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.ResponseBody

@Controller
class AssignmentDetail {
	final static Logger logger = Logger.getLogger(typeof(AssignmentDetail).getName());
 	@Autowired IObjectFactory entities;
	@RequestMapping(value="/assignmentdetail")
	def String process(
		@RequestParam(value="assignmentId", defaultValue="") Long assignmentId,
		Model model
	) {
		logger.info('=========>AssignmentDetail.processs<============')
		logger.info('=========>AssignmentDetail.assignmentId<============'+assignmentId)
		
		var hpage = 'error'
		
		try{
			val a = entities.get(typeof(EngineerProjectAssignment), assignmentId);
			if(a == null){
				model.addAttribute("errormsg", "Invalid id");
			}
			else {
				model.addAttribute("assignmentId", assignmentId);
				model.addAttribute("name", a.getWorkOrder.getProjectJob.getJobName)
				model.addAttribute("orderId", a.getWorkOrder.getSid);
				model.addAttribute("startDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getStartDate));
				model.addAttribute("endDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getEndDate));
				println('=========>AssignmentDetail.Status<============'+a.getStatus)
				if (a.getStatus == EngineerProjectAssignmentStatus.executing) {
					model.addAttribute("summary", '请输入工作小结');
					model.addAttribute("status", 'write');
				} else {
					println('=========>AssignmentDetail.summary<============'+a.getDailySummary.getSummary)
					model.addAttribute("summary", a.getDailySummary.getSummary);
					model.addAttribute("status", 'read');
				}
				hpage = 'assignmentdetail'
			}
		} catch(Exception e){
			e.printStackTrace
		}
		hpage;
	}
}


@RestController
@Transactional
@ResponseBody
class CommitSummary {
	final static Logger logger = Logger.getLogger(typeof(CommitSummary).getName());
	@Autowired IObjectFactory entities;
	@RequestMapping(value="/commitsummary", method = RequestMethod.POST)
	
	def StatusResponse process(Long assignmentId, String summary) {
		logger.info('=========>CommitSummary.processs<============')
		logger.info('=========>CommitSummary.assignmentId<============'+assignmentId)
		logger.info('=========>CommitSummary.summary<============'+summary)
		
		val result = new StatusResponse()
		try{
			val a = entities.get(typeof(EngineerProjectAssignment), assignmentId);
			a.doSummary(summary)
			a.finish()
			result.success = true
		} catch(Exception e){
			result.success = false
			e.printStackTrace
		}
		return result
	}
}